using Microsoft.AspNetCore.Mvc;
using PandaWebAPI.Models.ef;
using PandaWebAPI.Services;

namespace PandaWebAPI.Controllers;

/// <summary>
/// 帖子标签控制器
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class PostTagController : ControllerBase
{
    private readonly IPostTagService _postTagService;

    public PostTagController(IPostTagService postTagService)
    {
        _postTagService = postTagService;
    }

    /// <summary>
    /// 获取所有帖子标签
    /// </summary>
    [HttpGet]
    public async Task<ActionResult<IEnumerable<PostTag>>> GetAll()
    {
        var postTags = await _postTagService.GetAllAsync();
        return Ok(postTags);
    }

    /// <summary>
    /// 根据ID获取帖子标签
    /// </summary>
    [HttpGet("{id}")]
    public async Task<ActionResult<PostTag>> GetById(Guid id)
    {
        var postTag = await _postTagService.GetByIdAsync(id);
        if (postTag == null)
        {
            return NotFound();
        }
        return Ok(postTag);
    }

    /// <summary>
    /// 根据帖子ID获取标签
    /// </summary>
    [HttpGet("post/{postId}")]
    public async Task<ActionResult<IEnumerable<PostTag>>> GetByPostId(Guid postId)
    {
        var postTags = await _postTagService.GetByPostIdAsync(postId);
        return Ok(postTags);
    }

    /// <summary>
    /// 根据标签ID获取帖子
    /// </summary>
    [HttpGet("tag/{tagId}")]
    public async Task<ActionResult<IEnumerable<PostTag>>> GetByTagId(Guid tagId)
    {
        var postTags = await _postTagService.GetByTagIdAsync(tagId);
        return Ok(postTags);
    }

    /// <summary>
    /// 创建帖子标签
    /// </summary>
    [HttpPost]
    public async Task<ActionResult<PostTag>> Create(PostTag postTag)
    {
        var createdPostTag = await _postTagService.CreateAsync(postTag);
        return CreatedAtAction(nameof(GetById), new { id = createdPostTag.PostTagId }, createdPostTag);
    }

    /// <summary>
    /// 更新帖子标签
    /// </summary>
    [HttpPut("{id}")]
    public async Task<IActionResult> Update(Guid id, PostTag postTag)
    {
        if (id != postTag.PostTagId)
        {
            return BadRequest();
        }

        await _postTagService.UpdateAsync(postTag);
        return NoContent();
    }

    /// <summary>
    /// 删除帖子标签
    /// </summary>
    [HttpDelete("{id}")]
    public async Task<IActionResult> Delete(Guid id)
    {
        await _postTagService.DeleteAsync(id);
        return NoContent();
    }
} 